<template>
  <div>
    <van-nav-bar class="page-nav-bar" title="登录">
      <template v-slot:left>
        <van-icon name="arrow-left" @click="$router.go(-1)"></van-icon>
      </template>
    </van-nav-bar>
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        name="mobile"
        placeholder="请填写手机号"
        v-model="user.mobile"
        :rules="userFormRules.mobile"
        maxlength="11"
        type="number"
        ><template v-slot:left-icon>
          <i class="iconfont iconshouji"></i></template
      ></van-field>
      <van-field
        name="code"
        placeholder="请填写验证码"
        v-model="user.code"
        :rules="userFormRules.code"
        maxlength="6"
        type="number"
        ><template v-slot:left-icon>
          <i class="iconfont iconyanzhengma"></i>
        </template>

        <template #button>
          <van-count-down
            @finish="isCountDownShow = false"
            v-if="isCountDownShow"
            :time="60000"
            format="ss s"
          />
          <van-button
            v-else
            native-type="button"
            class="send-sms-btn"
            round
            size="small"
            @click="onSendSms"
          >
            获取验证码</van-button
          >
        </template></van-field
      >
      <div class="login-btn-wrap">
        <van-button
          @click="onSubmit"
          class="login-btn"
          block
          type="info"
          native-type="submit"
        >
          登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
import { login, sendSms } from '@/api/user.js'
export default {
  data() {
    return {
      isCountDownShow: false,
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/,
            message: '填写正确手机号'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '填写正确验证码'
          }
        ]
      },
      user: { mobile: '', code: '' }
    }
  },
  methods: {
    async onSendSms() {
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.log('验证失败', err)
      }
      this.isCountDownShow = true
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，请稍后重试')
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    },
    async onSubmit() {
      const user = this.user
      this.$toast.loading({
        message: '加载中...',
        duration: 0,
        forbidClick: true
      })
      try {
        const { data: res } = await login(user)
        this.$toast.success({ message: '登录成功', position: 'top' })
        this.$store.commit('setToken', res.data)
        this.$router.back()
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail({ message: '登录失败', position: 'top' })
          console.log('登录失败', err)
        }
      }
    }
  }
}
</script>
<style lang="less" scoped>
.iconfont {
  font-size: 37px;
}
.send-sms-btn {
  width: 152px;
  height: 46px;

  background-color: #ededed;
  font-size: 22px !important;
  color: #666;
  padding: 0;
}
.login-btn-wrap {
  padding: 53px 33px;
  .login-btn {
    background-color: #6db4fb;
    border: none;
  }
}
</style>
